home *** CD-ROM | disk | FTP | other *** search
/ Macworld Expo - Develope…Central & Net Innovations / Developer Central and Net Innovators (MacWorld Expo) (January 1999).iso / Developer Central / Bowers Development / Demo AppMaker / Examples / PowerPlant / Gadgets / CGadgetsDoc.cp < prev    next >
Encoding:
Text File  |  1998-10-11  |  5.5 KB  |  248 lines  |  [TEXT/CWIE]

  1. // CGadgetsDoc.cp -- Document methods
  2.  
  3. #include "CGadgetsDoc.h"
  4. #include "CGadgetsData.h"
  5.  
  6. #include "DDocData.h"
  7. #include "CButtons.h"
  8. #include "DDocData.h"
  9. #include "CTabbedPanel.h"
  10. #include "DDocData.h"
  11. #include "CEditText.h"
  12. #include "GadgetsCmds.h"
  13.  
  14. #include <LFile.h>
  15. #include <LPlaceHolder.h>
  16. #include <LPrintout.h>
  17. #include <LString.h>
  18. #include <LWindow.h>
  19. #include <TArrayIterator.h>
  20. #include <UWindows.h>
  21.  
  22. const ResIDT    prto_PrintView        = 201;
  23. const ResIDT    STRx_Untitled        = 128;
  24.  
  25. // ---------------------------------------------------------------------------
  26. //        • CGadgetsDoc
  27. // ---------------------------------------------------------------------------
  28.  
  29. CGadgetsDoc::CGadgetsDoc(
  30.     LCommander    *inSuper)
  31.     : LSingleDoc(inSuper)
  32. {
  33.     mData = new CGadgetsData();
  34. }
  35.  
  36. // ---------------------------------------------------------------------------
  37. //        • ~CGadgetsDoc
  38. // ---------------------------------------------------------------------------
  39. //    Destructor
  40. //
  41.  
  42. CGadgetsDoc::~CGadgetsDoc()
  43. {
  44.     // Delete all SubCommanders - copied from LCommander
  45.     // This fixes a bug that existed in CW11 and prior.
  46.     // It appears that the bug is fixed in CW Pro 1,
  47.     // so this code probably is no longer needed.
  48.     TArrayIterator<LCommander*> iterator(mSubCommanders, LArrayIterator::from_End);
  49.     LCommander*        theSub;
  50.     while (iterator.Previous (theSub)) {
  51.         mSubCommanders.RemoveItemsAt (1, iterator.GetCurrentIndex());
  52.         delete theSub;
  53.     }
  54.     mWindow = nil;
  55.  
  56.     delete mData;
  57. }
  58.  
  59. //----------
  60. void
  61. CGadgetsDoc::newFile()
  62. {
  63.     mData->NewData();
  64.  
  65.     MakeWindows();
  66.  
  67.     NameNewDoc();        // Set name of untitled window
  68. }
  69.  
  70. //----------
  71. void
  72. CGadgetsDoc::openFile(
  73.     FSSpec        *inFileSpec)
  74. {
  75.     mData->OpenData (inFileSpec);
  76.  
  77.     MakeWindows();
  78.  
  79.     if (mWindow != nil) {
  80.         mWindow->SetDescriptor (inFileSpec->name);
  81.     }
  82.     mIsSpecified = true;
  83. }
  84.  
  85. // ---------------------------------------------------------------------------
  86. //        • MakeWindows
  87. // ---------------------------------------------------------------------------
  88.  
  89. void
  90. CGadgetsDoc::MakeWindows()
  91. {
  92.     DDocData*        docData = mData->GetDocData ();
  93.  
  94.     mButtons = CButtons::CreateButtons(this, docData);
  95.     mTabbedPanel = CTabbedPanel::CreateTabbedPanel(this, docData);
  96.     mEditText = CEditText::CreateEditText(this, docData);
  97.  
  98.     mWindow = mButtons;
  99. }
  100.  
  101. // ---------------------------------------------------------------------------
  102. //        • NameNewDoc
  103. // ---------------------------------------------------------------------------
  104. //    Name a new, untitled document window
  105. //
  106. //    Untitled windows start with "untitled", then "untitled 1",
  107. //    "untitled 2", etc. Old numbers are reused, so there won't be
  108. //    gaps in the numbering.
  109. //
  110. //    This routine uses a STR# resource to store the "untitled" string,
  111. //    which can be localized to different languages. The first string
  112. //    is "untitled" and the second is "untitled " (trailing space),
  113. //    which is used when appending a number to the name.
  114.  
  115. void
  116. CGadgetsDoc::NameNewDoc()
  117. {
  118.     // Start with the default name("untitled")
  119.     Str255    name;
  120.     ::GetIndString(name, STRx_Untitled, 1);
  121.  
  122.     long    num = 0;
  123.     while (UWindows::FindNamedWindow(name) != nil) {
  124.  
  125.             // An existing window has the current name
  126.             // Increment counter and try again
  127.  
  128.         ::GetIndString(name, STRx_Untitled, 2);
  129.         num++;
  130.         Str15    numStr;
  131.         ::NumToString(num, numStr);
  132.         LString::AppendPStr(name, numStr);
  133.     }
  134.  
  135.     mWindow->SetDescriptor(name);        // Finally, set window title
  136. }
  137. // ---------------------------------------------------------------------------
  138. //        • IsModified
  139. // ---------------------------------------------------------------------------
  140. //    Return whether the Document has changed since the last save
  141.  
  142. Boolean
  143. CGadgetsDoc::IsModified()
  144. {
  145.     mIsModified = mData->IsDirty();
  146.  
  147.     return mIsModified;
  148. }
  149.  
  150. // ---------------------------------------------------------------------------
  151. //        • DoAESave
  152. // ---------------------------------------------------------------------------
  153. //    Save Document in the specified file with the specified file type
  154. //
  155. //    If file type is fileType_Default, use the normal file type for
  156. //    this document
  157.  
  158. void
  159. CGadgetsDoc::DoAESave(
  160.     FSSpec    &inFileSpec,
  161.     OSType    inFileType)
  162. {
  163.     mData->DoSaveAs(&inFileSpec);                // Write out data
  164.                                         // Change window name
  165.     mWindow->SetDescriptor(inFileSpec.name);
  166. }
  167.  
  168. //----------
  169. void
  170. CGadgetsDoc::DoSave()
  171. {
  172.     mData->DoSave();
  173. }
  174.  
  175. //----------
  176. void
  177. CGadgetsDoc::DoRevert()
  178. {
  179.     mData->DoRevert();
  180. }
  181.  
  182. // ---------------------------------------------------------------------------
  183. //        • DoPrint
  184. // ---------------------------------------------------------------------------
  185. //    Print the contents of the Document
  186.  
  187. void
  188. CGadgetsDoc::DoPrint()
  189. {
  190.     LPrintout        *thePrintout = LPrintout::CreatePrintout(prto_PrintView);
  191.     LPlaceHolder    *textPlace = (LPlaceHolder *)
  192.                                     thePrintout->FindPaneByID('TBox');
  193. //!    textPlace->InstallOccupant(mTextView, atNone);
  194.  
  195.  
  196.     thePrintout->DoPrintJob();
  197.     delete thePrintout;
  198. }
  199.  
  200. //----------
  201. Boolean
  202. CGadgetsDoc::ObeyCommand(
  203.     CommandT    inCommand,
  204.     void*        ioParam)
  205. {
  206.     Boolean        cmdHandled = true;
  207.  
  208.     if (inCommand < 0) {
  209.         // modal dialog has finished
  210.  
  211.         switch (-inCommand) {
  212.         }
  213.  
  214.     } else {
  215.         switch (inCommand) {
  216.  
  217.         default:
  218.                 cmdHandled = LSingleDoc::ObeyCommand(inCommand, ioParam);
  219.             break;
  220.         }
  221.     }
  222.  
  223.     return cmdHandled;
  224. }
  225.  
  226. //----------
  227. void
  228. CGadgetsDoc::FindCommandStatus(
  229.     CommandT    inCommand,
  230.     Boolean        &outEnabled,
  231.     Boolean        &outUsesMark,
  232.     Char16        &outMark,
  233.     Str255        outName)
  234. {
  235.     outUsesMark = false;
  236.  
  237.     switch (inCommand) {
  238.  
  239.     // +++ Add cases here for the commands you handle
  240.  
  241.  
  242.     default:
  243.             LSingleDoc::FindCommandStatus(inCommand, outEnabled,
  244.                                             outUsesMark, outMark, outName);
  245.         break;
  246.     }
  247. }
  248.